package com.gla.market.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.gla.market.R;
import com.gla.market.entity.ShopCart;
import com.gla.market.entity.ShopInCart;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import org.apache.commons.lang3.StringUtils;

import java.text.DecimalFormat;
import java.util.List;

/**
 * 订单里的商店适配器
 */
public class OrderShopAdapter extends RecyclerView.Adapter<OrderShopAdapter.ViewHolder> {

    private List<ShopInCart> shopBeanList;

    private Context context;

    public OrderShopAdapter(Context context) {
        this.context = context;
    }

    public void setShopBeanList(List<ShopInCart> shopBeanList) {
        this.shopBeanList = shopBeanList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_order_shop, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ShopInCart shopInCart = shopBeanList.get(position);

        holder.tv_shopName.setText(shopInCart.getName());
        holder.tv_remarks.setText(StringUtils.isBlank(shopInCart.getRemarks()) ? "无备注":shopInCart.getRemarks());
        holder.tv_remarks.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent("com.gla.market.popwindow");
                intent.putExtra("shop_id",shopInCart.getId());
                context.sendBroadcast(intent);
            }
        });
        OrderItemAdapter adapter = new OrderItemAdapter(context,shopInCart.getList());
        holder.rv_cartList.setLayoutManager(new LinearLayoutManager(context));
        holder.rv_cartList.setAdapter(adapter);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return shopBeanList == null ? 0 : shopBeanList.size();
    }

    //初始化控件
    class ViewHolder extends RecyclerView.ViewHolder {

        TextView tv_shopName;
        RecyclerView rv_cartList;
        TextView tv_remarks;

        public ViewHolder(View itemView) {
            super(itemView);
            tv_shopName = itemView.findViewById(R.id.item_order_shop_name);
            rv_cartList = itemView.findViewById(R.id.order_cart_list);
            tv_remarks = itemView.findViewById(R.id.order_remarks);
        }
    }
}
